﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.ENT;
using SIS.BLL;

public partial class school_timetable : System.Web.UI.Page
{
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (DateTime.Now.Year >= 2012)
            {
                for (int y = 2012; y <= DateTime.Now.Year + 3; y++)
                {
                    ddlPeriodStartYear.Items.Add(y.ToString());
                }

                ddlPeriodStartYear.Text = DateTime.Now.Year.ToString();
            }
            else
            {
                for (int y = DateTime.Now.Year; y <= DateTime.Now.Year + 3; y++)
                {
                    ddlPeriodStartYear.Items.Add(y.ToString());
                }

                ddlPeriodStartYear.Text = DateTime.Now.Year.ToString();
            }

            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            for (int h = 0; h < 24; h++)
            {
                ddlStartHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
            }

            for (int m = 0; m < 60; m += 5)
            {
                ddlStartMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
            }

            for (int h = 0; h <= 6; h++)
            {
                ddlSectionDurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime1DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime2DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime3DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime4DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime5DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime6DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime7DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime8DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime9DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));
                ddlBreakTime10DurationHour.Items.Add(new ListItem(h.ToString("00"), h.ToString()));

            }

            for (int m = 0; m < 60; m += 5)
            {
                ddlSectionDurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime1DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime2DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime3DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime4DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime5DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime6DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime7DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime8DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime9DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
                ddlBreakTime10DurationMin.Items.Add(new ListItem(m.ToString("00"), m.ToString()));
            }

            ddlSectionBreak.Items.Add(new ListItem("No Break", "0"));

            for (int b = 1; b <= 4; b++)
            {
                if (b == 1)
                {
                    ddlSectionBreak.Items.Add(new ListItem("Every " + b.ToString() + " section", b.ToString()));
                }
                else
                {
                    ddlSectionBreak.Items.Add(new ListItem("Every " + b.ToString() + " sections", b.ToString()));
                }
            }

            for (int s = 1; s <= 20; s++)
            {
                ddlNoOfSection.Items.Add(s.ToString());
            }

            ClearFields();
        }
    }


    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("TimeTable");

        if (Page.IsValid)
        {
            if (ddlSectionDurationHour.Text == "0" && ddlSectionDurationMin.Text == "0")
            {
                lblMessage.Text = "Section duration is invalid.";
                return;
            }
            else
            {
                TimeTable _t = new TimeTable();
                _t.TimeTableKey = Convert.ToInt32(lblTimeTableKey.Text);
                _t.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
                _t.MajorKey = Convert.ToInt32(ddlSchoolMajor.Text);
                _t.StandardLevelKey = Convert.ToInt32(ddlStandardLevel.Text);
                _t.LocationKey = Convert.ToInt32(ddlLocation.Text);
                _t.EducationPeriodlKey = Convert.ToInt32(ddlPeriod.Text);
                _t.DayGroupCode = Convert.ToInt16(ddlDayGroup.Text);
                _t.StartTime = new TimeSpan(Convert.ToInt32(ddlStartHour.Text), Convert.ToInt32(ddlStartMin.Text), 0);
                _t.BreakTimeNo = Convert.ToInt16(ddlSectionBreak.Text);
                _t.SectionDuration = new TimeSpan(Convert.ToInt32(ddlSectionDurationHour.Text), Convert.ToInt32(ddlSectionDurationMin.Text), 0);
                _t.NoOfSection = Convert.ToInt16(ddlNoOfSection.Text);
                _t.Status = true;
                _t.UserID = 0;

                if (divBreakTime1Duration.Visible)
                {
                    _t.BreakTime1Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime1DurationHour.Text), Convert.ToInt32(ddlBreakTime1DurationMin.Text), 0);
                }

                if (divBreakTime2Duration.Visible)
                {
                    _t.BreakTime2Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime2DurationHour.Text), Convert.ToInt32(ddlBreakTime2DurationMin.Text), 0);
                }

                if (divBreakTime3Duration.Visible)
                {
                    _t.BreakTime3Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime3DurationHour.Text), Convert.ToInt32(ddlBreakTime3DurationMin.Text), 0);
                }

                if (divBreakTime4Duration.Visible)
                {
                    _t.BreakTime4Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime4DurationHour.Text), Convert.ToInt32(ddlBreakTime4DurationMin.Text), 0);
                }

                if (divBreakTime5Duration.Visible)
                {
                    _t.BreakTime5Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime5DurationHour.Text), Convert.ToInt32(ddlBreakTime5DurationMin.Text), 0);
                }

                if (divBreakTime6Duration.Visible)
                {
                    _t.BreakTime6Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime6DurationHour.Text), Convert.ToInt32(ddlBreakTime6DurationMin.Text), 0);
                }

                if (divBreakTime7Duration.Visible)
                {
                    _t.BreakTime7Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime7DurationHour.Text), Convert.ToInt32(ddlBreakTime7DurationMin.Text), 0);
                }

                if (divBreakTime8Duration.Visible)
                {
                    _t.BreakTime8Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime8DurationHour.Text), Convert.ToInt32(ddlBreakTime8DurationMin.Text), 0);
                }

                if (divBreakTime9Duration.Visible)
                {
                    _t.BreakTime9Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime9DurationHour.Text), Convert.ToInt32(ddlBreakTime9DurationMin.Text), 0);
                }

                if (divBreakTime10Duration.Visible)
                {
                    _t.BreakTime10Duration = new TimeSpan(Convert.ToInt32(ddlBreakTime10DurationHour.Text), Convert.ToInt32(ddlBreakTime10DurationMin.Text), 0);
                }
                

                if (TimeTableManager.Save(_t) > 0)
                {
                    lblTimeTableKey.Text = _t.TimeTableKey.ToString();
                    lblMessage.Text = "Saved Successfully.";
                    gvTimeTable.DataBind();
                    //ClearFields();
                }
                else
                {
                    lblMessage.Text = "Saving failed.";
                }
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(lblTimeTableKey.Text) > 0)
        {
            //Response.Redirect("timetable-details.aspx?ttid=" + lblTimeTableKey.Text);
            if (TimeTableManager.Delete(Convert.ToInt32(lblTimeTableKey.Text)))
            {
                lblMessage.Text = "Deleted successfully.";
                gvTimeTable.DataBind();
                ClearFields();
            }
        }
        else
        {
            lblMessage.Text = "Please seleect a timetable";
        }
    }

    private void ClearFields()
    {
        lblTimeTableKey.Text = "-1";
        ddlDayGroup.SelectedIndex = 0;
        ddlStartHour.SelectedIndex = 0;
        ddlStartMin.SelectedIndex = 0;
        ddlNoOfSection.SelectedIndex = 0;
        ddlSectionBreak.SelectedIndex = 0;
        lblMessage.Text = string.Empty;
        GetTimeList();
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ClearFields();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ClearFields();
    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        ddlStandardLevel_SelectedIndexChanged(sender, e);
        ddlLocation.DataBind();
        ddlLocation_SelectedIndexChanged(sender, e);
        ddlPeriod.DataBind();
        ClearFields();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void ddlPeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void gvTimeTable_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gvTimeTable.SelectedIndex >= 0)
        {
            int _timetableKey = Convert.ToInt32(gvTimeTable.SelectedDataKey[0].ToString());
            TimeTable _t = new TimeTable();
            _t = TimeTableManager.GetItem(_timetableKey);

            if (_t != null)
            {
                lblTimeTableKey.Text = _t.TimeTableKey.ToString();
                ddlDayGroup.Text = _t.DayGroupCode.ToString();
                
                ddlStartHour.Text = _t.StartTime.Hours.ToString();
                ddlStartMin.Text = _t.StartTime.Minutes.ToString();

                ddlSectionDurationHour.Text = _t.SectionDuration.Hours.ToString();
                ddlSectionDurationMin.Text = _t.SectionDuration.Minutes.ToString();

                ddlNoOfSection.Text = _t.NoOfSection.ToString();
                ddlSectionBreak.Text = _t.BreakTimeNo.ToString();

                ddlBreakTime1DurationHour.Text = _t.BreakTime1Duration.Hours.ToString();
                ddlBreakTime1DurationMin.Text = _t.BreakTime1Duration.Minutes.ToString();

                ddlBreakTime2DurationHour.Text = _t.BreakTime2Duration.Hours.ToString();
                ddlBreakTime2DurationMin.Text = _t.BreakTime2Duration.Minutes.ToString();

                ddlBreakTime3DurationHour.Text = _t.BreakTime3Duration.Hours.ToString();
                ddlBreakTime3DurationMin.Text = _t.BreakTime3Duration.Minutes.ToString();

                ddlBreakTime4DurationHour.Text = _t.BreakTime4Duration.Hours.ToString();
                ddlBreakTime4DurationMin.Text = _t.BreakTime4Duration.Minutes.ToString();

                ddlBreakTime5DurationHour.Text = _t.BreakTime5Duration.Hours.ToString();
                ddlBreakTime5DurationMin.Text = _t.BreakTime5Duration.Minutes.ToString();

                ddlBreakTime6DurationHour.Text = _t.BreakTime6Duration.Hours.ToString();
                ddlBreakTime6DurationMin.Text = _t.BreakTime6Duration.Minutes.ToString();

                ddlBreakTime7DurationHour.Text = _t.BreakTime7Duration.Hours.ToString();
                ddlBreakTime7DurationMin.Text = _t.BreakTime7Duration.Minutes.ToString();

                ddlBreakTime8DurationHour.Text = _t.BreakTime8Duration.Hours.ToString();
                ddlBreakTime8DurationMin.Text = _t.BreakTime8Duration.Minutes.ToString();

                ddlBreakTime9DurationHour.Text = _t.BreakTime9Duration.Hours.ToString();
                ddlBreakTime9DurationMin.Text = _t.BreakTime9Duration.Minutes.ToString();

                ddlBreakTime10DurationHour.Text = _t.BreakTime10Duration.Hours.ToString();
                ddlBreakTime10DurationMin.Text = _t.BreakTime10Duration.Minutes.ToString();

                GetTimeList();
            }
        }
    }

    private void GetTimeList()
    {
        divBreakTime1Duration.Visible = false;
        divBreakTime2Duration.Visible = false;
        divBreakTime3Duration.Visible = false;
        divBreakTime4Duration.Visible = false;
        divBreakTime5Duration.Visible = false;
        divBreakTime6Duration.Visible = false;
        divBreakTime7Duration.Visible = false;
        divBreakTime8Duration.Visible = false;
        divBreakTime9Duration.Visible = false;
        divBreakTime10Duration.Visible = false;

        ddlTimeDesc.Items.Clear();
        ddlStartTime.Items.Clear();
        ddlEndTime.Items.Clear();

        int _hour = Convert.ToInt32(ddlStartHour.Text);
        int _min = Convert.ToInt32(ddlStartMin.Text);
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        _hour = Convert.ToInt32(ddlSectionDurationHour.Text);
        _min = Convert.ToInt32(ddlSectionDurationMin.Text);
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(ddlSectionBreak.Text);

        int _rowCount = Convert.ToInt32(ddlNoOfSection.Text);

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;

        for (int r = 0; r < _rowCount; r++)
        {
            tsStartTime = tsNextTime;
            
            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(ddlBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime1DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime1Duration.Text = "Break Time 1<br/>" + tsNextTime.ToString();
                    divBreakTime1Duration.Visible = true;
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(ddlBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime2DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime2Duration.Text = "Break Time 2<br/>" + tsNextTime.ToString();
                    divBreakTime2Duration.Visible = true;
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(ddlBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime3DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime3Duration.Text = "Break Time 3<br/>" + tsNextTime.ToString();
                    divBreakTime3Duration.Visible = true;
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(ddlBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime4DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime4Duration.Text = "Break Time 4<br/>" + tsNextTime.ToString();
                    divBreakTime4Duration.Visible = true;
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(ddlBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime5DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime5Duration.Text = "Break Time 5<br/>" + tsNextTime.ToString();
                    divBreakTime5Duration.Visible = true;
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(ddlBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime6DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime6Duration.Text = "Break Time 6<br/>" + tsNextTime.ToString();
                    divBreakTime6Duration.Visible = true;
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(ddlBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime7DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime7Duration.Text = "Break Time 7<br/>" + tsNextTime.ToString();
                    divBreakTime7Duration.Visible = true;
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(ddlBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime8DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime8Duration.Text = "Break Time 8<br/>" + tsNextTime.ToString();
                    divBreakTime8Duration.Visible = true;
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(ddlBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime9DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime9Duration.Text = "Break Time 9<br/>" + tsNextTime.ToString();
                    divBreakTime9Duration.Visible = true;
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(ddlBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(ddlBreakTime10DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    lblBreakTime10Duration.Text = "Break Time 10<br/>" + tsNextTime.ToString();
                    divBreakTime10Duration.Visible = true;
                }

                tsNextTime += tsTempBT;
                _currentBTNo += 1;
            }
            else
            {
                _ItemDesc = tsNextTime.ToString();
                tsNextTime += tsSectionDuration;
                _ItemDesc += " to " + tsNextTime.ToString();
            }

            ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
            ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));

            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                _currentBTCount = 0;
                r--;
                ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, "B" + _currentBTNo.ToString()));
            }
            else
            {
                _currentBTCount += 1;
                _currentSectionNo += 1;
                ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
            }
        }
    }

    protected void ddlStartHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSectionList();
        GetTimeList();
    }

    private void GetSectionList()
    {
        if (Convert.ToInt32(ddlSectionDurationHour.Text) > 0 || Convert.ToInt32(ddlSectionDurationMin.Text) > 0)
        {
            ddlNoOfSection.Items.Clear();
            TimeSpan _maxTime = new TimeSpan(26, 0, 0);
            TimeSpan _sectionDuration = new TimeSpan(Convert.ToInt32(ddlSectionDurationHour.Text), Convert.ToInt32(ddlSectionDurationMin.Text), 0);
            TimeSpan _startTime = new TimeSpan(Convert.ToInt32(ddlStartHour.Text), Convert.ToInt32(ddlStartHour.Text), 0);

            int _curSectionNo = 1;
            _startTime += _sectionDuration;

            while (_startTime < _maxTime && _curSectionNo <= 20)
            {
                ddlNoOfSection.Items.Add(_curSectionNo.ToString());
                _curSectionNo++;
                _startTime += _sectionDuration;
            }

            if (ddlNoOfSection.Items.Count > 0)
            {
                ddlNoOfSection.SelectedIndex = 0;
            }
        }
    }

    protected void ddlStartMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSectionList();
        GetTimeList();
    }

    protected void ddlSectionDurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSectionList();
        GetTimeList();
    }

    protected void ddlSectionDurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSectionList();
        GetTimeList();
    }

    protected void ddlNoOfSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        TimeSpan _sectionDuration = new TimeSpan(Convert.ToInt32(ddlSectionDurationHour.Text), Convert.ToInt32(ddlSectionDurationMin.Text), 0);
        if (_sectionDuration.Hours > 0 || _sectionDuration.Minutes > 3)
        {
            ddlSectionBreak.SelectedIndex = 0;
            GetTimeList();
        }
        else
        {
            ddlNoOfSection.SelectedIndex = 0;
            ddlSectionBreak.SelectedIndex = 0;
            ScriptManager.RegisterClientScriptBlock(ddlSectionBreak, ddlSectionBreak.GetType(), Guid.NewGuid().ToString(), "alert('Invalid section duration.');", true);
            GetTimeList();
        }
    }

    protected void ddlSectionBreak_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ddlNoOfSection.Text) > Convert.ToInt32(ddlSectionBreak.Text))
        {
            if (Convert.ToInt32(ddlSectionBreak.Text) > 0)
            {
                if (Convert.ToInt32(ddlNoOfSection.Text) / Convert.ToInt32(ddlSectionBreak.Text) > 10)
                {
                    ddlSectionBreak.SelectedIndex = 0;
                    ScriptManager.RegisterClientScriptBlock(ddlSectionBreak, ddlSectionBreak.GetType(), Guid.NewGuid().ToString(), "alert('Invalid break time. Please select less break time option.');", true);
                }
            }
        }
        else
        {
            ddlSectionBreak.SelectedIndex = 0;
            ScriptManager.RegisterClientScriptBlock(ddlSectionBreak, ddlSectionBreak.GetType(), Guid.NewGuid().ToString(), "alert('Invalid break time. Break time is more than No. Of Section.');", true);
        }

        GetTimeList();
    }

    protected void ddlBreakTime1DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime1DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime2DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime2DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime3DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime3DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime4DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime4DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime5DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime5DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime6DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime6DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime7DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime7DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime8DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime8DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime9DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime9DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }
    protected void ddlBreakTime10DurationHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }

    protected void ddlBreakTime10DurationMin_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTimeList();
    }

    protected void ddlPeriodStartYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ddlPeriod_SelectedIndexChanged(sender, e);
        gvTimeTable.DataBind();
    }
}
